package com.eudon.design_pattern.behavioral.interpreter;

/**
 * @author eudon
 * @description 规则解析器，将字符串规则解析成表达式树
 * @date 2025/9/3
 * @email syd19970616@gmail.com
 * @address <a href="https://gitee.com/eudon">Gitee</a>
 */
public class RuleParser {
    
    /**
     * 解析规则字符串，构建表达式树
     * @param ruleString 规则字符串，如："接口A调用次数 > 100 并且 接口A失败次数 > 10"
     * @return 解析后的规则表达式
     */
    public static RuleExpression parse(String ruleString) {
        // 简化解析逻辑，支持基本的AND和OR操作
        if (ruleString.contains("并且")) {
            return parseAndExpression(ruleString);
        } else if (ruleString.contains("或者")) {
            return parseOrExpression(ruleString);
        } else {
            return parseCompareExpression(ruleString);
        }
    }
    
    /**
     * 解析AND表达式
     */
    private static RuleExpression parseAndExpression(String ruleString) {
        String[] parts = ruleString.split("并且", 2);
        RuleExpression left = parse(parts[0].trim());
        RuleExpression right = parse(parts[1].trim());
        return new AndExpression(left, right);
    }
    
    /**
     * 解析OR表达式
     */
    private static RuleExpression parseOrExpression(String ruleString) {
        String[] parts = ruleString.split("或者", 2);
        RuleExpression left = parse(parts[0].trim());
        RuleExpression right = parse(parts[1].trim());
        return new OrExpression(left, right);
    }
    
    /**
     * 解析比较表达式
     * 支持格式："接口A调用次数 > 100"、"接口B失败次数 >= 10"、"接口C失败率 > 0.1"
     */
    private static RuleExpression parseCompareExpression(String ruleString) {
        // 提取接口名称
        String apiName = extractApiName(ruleString);
        
        // 提取指标类型
        String metricType = extractMetricType(ruleString);
        
        // 提取操作符和阈值
        String[] operatorAndThreshold = extractOperatorAndThreshold(ruleString);
        String operator = operatorAndThreshold[0];
        double threshold = Double.parseDouble(operatorAndThreshold[1]);
        
        return new CompareExpression(apiName, metricType, operator, threshold);
    }
    
    /**
     * 提取接口名称
     */
    private static String extractApiName(String ruleString) {
        // 查找"接口"关键字后的接口名称
        int startIndex = ruleString.indexOf("接口") + 2;
        int endIndex = ruleString.indexOf("调用次数");
        if (endIndex == -1) {
            endIndex = ruleString.indexOf("失败次数");
        }
        if (endIndex == -1) {
            endIndex = ruleString.indexOf("失败率");
        }
        return ruleString.substring(startIndex, endIndex);
    }
    
    /**
     * 提取指标类型
     */
    private static String extractMetricType(String ruleString) {
        if (ruleString.contains("调用次数")) {
            return "调用次数";
        } else if (ruleString.contains("失败次数")) {
            return "失败次数";
        } else if (ruleString.contains("失败率")) {
            return "失败率";
        }
        throw new IllegalArgumentException("无法识别的指标类型: " + ruleString);
    }
    
    /**
     * 提取操作符和阈值
     */
    private static String[] extractOperatorAndThreshold(String ruleString) {
        String[] operators = {">=", "<=", "==", ">", "<"};
        
        for (String op : operators) {
            if (ruleString.contains(" " + op + " ")) {
                String[] parts = ruleString.split(" " + op + " ");
                return new String[]{op, parts[1].trim()};
            }
        }
        
        throw new IllegalArgumentException("无法识别的操作符: " + ruleString);
    }
}